//
//  TTUtil.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/17.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import Foundation
import UIKit
import ObjectiveC

func SET_LINEVIEW(view:UIView) {
    let lineView = UIView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 1))
    lineView.backgroundColor = RGB_Same(240, 1)
    view.addSubview(lineView)
}

func SCREEN_SCALE_355X(x:CGFloat) -> CGFloat {
    return x*(UIScreen.main.bounds.size.width - 20) / 355.0
}
func SCREEN_SCALE_X(x:CGFloat) -> CGFloat {
    return x*UIScreen.main.bounds.size.width / 375.0
}
func SCREEN_SCALE_Y(y:CGFloat) -> CGFloat {
    return y*UIScreen.main.bounds.size.height / 667.0
}

func VIEW_X(view: UIView) -> CGFloat  {
    return view.frame.origin.x
}
func VIEW_Y(view: UIView) -> CGFloat {
    return view.frame.origin.y
}
func VIEW_WIDTH(view: UIView) -> CGFloat {
    return view.frame.size.width
}
func VIEW_HEIGHT(view: UIView) -> CGFloat {
    return view.frame.size.height
}

func FRAME_X(frame:CGRect) -> CGFloat {
    return frame.origin.x
}
func FRAME_Y(frame:CGRect) -> CGFloat {
    return frame.origin.y
}
func FRAME_W(frame:CGRect) -> CGFloat {
    return frame.size.width
}
func FRAME_H(frame:CGRect) -> CGFloat {
    return frame.size.height
}

func LoadXibView(name:String) ->UIView{  return Bundle.main.loadNibNamed(name, owner: nil, options: nil)?.first as! UIView}
func LoadXibViewController(name:String) ->UIViewController{  return Bundle.main.loadNibNamed(name, owner: nil, options: nil)?.first as! UIViewController}
func LoadViewControllerFrom(storyboard:String,identifier:String) ->UIViewController{
    let storyboard = UIStoryboard(name: storyboard, bundle: Bundle.main)
    let viewController = storyboard.instantiateViewController(withIdentifier: identifier)
    return viewController
}
func loadViewController(viewControllerName: String) -> UIViewController {
    let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
    let clsName = namespace + "." + viewControllerName
    let cls = NSClassFromString(clsName) as! UIViewController.Type
    return cls.init()
}

func PUSHVIEWCONTROLLER(_ viewcontroller:UIViewController){
    viewcontroller.hidesBottomBarWhenPushed = true
    print(viewcontroller.nibName ?? "")
    
    (AppDelegate.getAppDelegate().tabbarVC?.selectedViewController as! UINavigationController).pushViewController(viewcontroller, animated: true)
}

func ISLOGIN_PUSHVIEWCONTROLLER(_ viewcontroller:UIViewController){
    if !String.getUserIdToString() {
        let vc = LoginVc()
        PRESENTVIEWCONTROLLER(vc)
    } else {
        viewcontroller.hidesBottomBarWhenPushed = true
        print(viewcontroller.nibName ?? "")
        
        (AppDelegate.getAppDelegate().tabbarVC?.selectedViewController as! UINavigationController).pushViewController(viewcontroller, animated: true)
    }
    
}

func POPTOVIEWCONTROLLER(_ viewController: String){
    let nav = (AppDelegate.getAppDelegate().tabbarVC?.selectedViewController as! UINavigationController)
    let viewcontrollers = nav.viewControllers
    
    let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
    let clsName = namespace + "." + viewController
    let cls = NSClassFromString(clsName) as! UIViewController.Type
    
    for vc in viewcontrollers {
        
        if vc.isKind(of: cls) {
            nav.popToViewController(vc, animated: true)
            break;
        }
    }
}

func ISPOPTOVIEWCONTROLLERSUCCEED(_ viewController: String) -> Bool{
    let nav = (AppDelegate.getAppDelegate().tabbarVC?.selectedViewController as! UINavigationController)
    let viewcontrollers = nav.viewControllers
    
    let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
    let clsName = namespace + "." + viewController
    let cls = NSClassFromString(clsName) as! UIViewController.Type
    
    for vc in viewcontrollers {
        
        if vc.isKind(of: cls) {
            nav.popToViewController(vc, animated: true)
            return true
        }
    }
    
    return false
}

func DELVIEWCONTROLLERSUCCEED(_ viewController: String){
    let nav = (AppDelegate.getAppDelegate().tabbarVC?.selectedViewController as! UINavigationController)
    var viewcontrollers = nav.viewControllers
    
    let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
    let clsName = namespace + "." + viewController
    let cls = NSClassFromString(clsName) as! UIViewController.Type
    
    for vc in viewcontrollers.reversed() {
        
        if vc.isKind(of: cls) == false {
            viewcontrollers.removeLast()
        } else {
            break
        }
    }
    
    if viewcontrollers.count != 0 {
        nav.viewControllers = viewcontrollers
    }
}

func TIMESTMP() ->UInt64 {
    return UInt64(Date().timeIntervalSince1970)
}

func POPTOVIEWCONTROLLERSUPER(_ viewController: String){
    let nav = (AppDelegate.getAppDelegate().tabbarVC?.selectedViewController as! UINavigationController)
    let viewcontrollers = nav.viewControllers
    
    let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
    let clsName = namespace + "." + viewController
    let cls = NSClassFromString(clsName) as! UIViewController.Type
    
    
    for index in 0..<viewcontrollers.count {
        let vc = viewcontrollers[index]
        if vc.isKind(of: cls) {
            if index == 0 {
                nav.popToViewController(vc, animated: true)
            } else {
                nav.popToViewController(viewcontrollers[index - 1], animated: true)
            }
            break;
        }
    }
}

func PRESENTVIEWCONTROLLER(_ viewcontroller:UIViewController){
    (AppDelegate.getAppDelegate().tabbarVC?.selectedViewController as! UINavigationController).present(viewcontroller, animated: true) {
    }
}

func RGBA(r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat) -> UIColor {
    return UIColor.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
    
}
func RGB(_ r:CGFloat,_ g:CGFloat,_ b:CGFloat,_ a:CGFloat = 1) -> UIColor {
    return UIColor.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
}
func RGB_Same(_ rgb:CGFloat,_ a:CGFloat = 1) -> UIColor {
    return UIColor.init(red: rgb/255.0, green: rgb/255.0, blue: rgb/255.0, alpha: a)
}
func RGBHEX(_ rgbValue: UInt) -> UIColor {
    return UIColor(
        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
        alpha: CGFloat(1.0)
    )
}

func FontSet(_ name:String, _ size:CGFloat) -> UIFont? {
    return UIFont.init(name: name, size: size)
}

func IMAGE(name:String) -> UIImage {
    return UIImage.init(named: name)!
}

func FRAME_Int(_ x:Int,_ y:Int,_ width:Int,_ height:Int) -> CGRect {
    return CGRect.init(x: x, y: y, width: width, height: height)
}

func FRAME_CGFloat(_ x:CGFloat,_ y:CGFloat,_ width:CGFloat,_ height:CGFloat) -> CGRect {
    return CGRect.init(x: x, y: y, width: width, height: height)
}

func FONT(_ size:CGFloat) -> UIFont {
    return UIFont.systemFont(ofSize: size)
}

func URLSTR(_ str: String) -> URL {
    return URL(string: str)!
}

func NIBNAME(_ name:String) -> UINib {
    return UINib(nibName:name, bundle:nil)
}

func StringIncludeChineseToString(_ string:String) -> String {
    for (_, value) in string.characters.enumerated() {
        if ("\u{4E00}" <= value  && value <= "\u{9FA5}") {
            let imageUrl = string.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
            return imageUrl ?? ""
        }
    }
    
    return string
}

func validMobile(mobile: String) -> Bool {
    return TTValidator.isTelNumber(num: mobile)
}

func limitInput(digit: NSInteger, input: String) -> String {
    var retStr = input
    if input.characters.count > digit {
        retStr = input.substring(to: input.index(input.startIndex, offsetBy: digit))
    }
    
    return retStr
}

func validPassword(password: String) -> Bool {
    return TTValidator.isDigital(password, 6)
}

func validPrice(money: String) -> Bool {
    if TTValidator.validatorPrice(money) {
        return TTValidator.validatorPrice0to7(money)
    } else {
        return false
    }
}


func validValidateCode(validateCode: String) -> Bool {
    return TTValidator.isDigital(validateCode, 4)
}


func validInviteCode(validateCode: String) -> Bool {
    return TTValidator.isDigital(validateCode, 4)
}

//func URLStringIncludeChineseToString(_ string:String) -> URL {
//    for (_, value) in string.characters.enumerated() {
//        if ("\u{4E00}" <= value  && value <= "\u{9FA5}") {
//            let imageUrl = string.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
//            return URL(string: string)!
//        }
//    }
//
//    return URL(string: string)!
//}

func AddHttpsHeader(string:String?) -> String {
    if string != nil {
        let newUrl = "http://source.blistening.cn/bt_source/ttbt/" + string!
        return newUrl
    } else {
        return ""
    }
}

